<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Implementing the BinaryLight device</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="gupnp.browsing.html">Browsing devices and services</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.gupnp.html">Gupnp 函数</a></div>
 <div class="up"><a href="gupnp.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="gupnp.binary-light" class="section">
  <h2 class="title">Implementing the BinaryLight device</h2>
  <p class="para">
   This is an example of UPnP device/service, implementing the BinaryLight 
   device and SwitchPower services to emulate a light switch.
  </p>
  <p class="para">
   The user interface was purposely simplified in order to show basic 
   concepts and methods.
  </p>
  <p class="para">
   <div class="example" id="example-4245">
    <p><strong>Example #1 Implementing light server</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">/*&nbsp;SetTarget&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">set_target_cb</span><span style="color: #007700">(</span><span style="color: #0000BB">$service</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$action</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arg</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Get&nbsp;the&nbsp;new&nbsp;target&nbsp;value&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$target&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gupnp_service_action_get</span><span style="color: #007700">(</span><span style="color: #0000BB">$action</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'NewTargetValue'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">GUPNP_TYPE_BOOLEAN</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;If&nbsp;the&nbsp;new&nbsp;target&nbsp;doesn't&nbsp;match&nbsp;the&nbsp;current&nbsp;status,&nbsp;change&nbsp;the&nbsp;status&nbsp;and<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;emit&nbsp;a&nbsp;notification&nbsp;that&nbsp;the&nbsp;status&nbsp;has&nbsp;changed.&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$target&nbsp;</span><span style="color: #007700">!=&nbsp;</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'status'</span><span style="color: #007700">])&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'status'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$target</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">gupnp_service_notify</span><span style="color: #007700">(</span><span style="color: #0000BB">$service</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Status'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">GUPNP_TYPE_BOOLEAN</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'status'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"The&nbsp;light&nbsp;is&nbsp;now&nbsp;%s.\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'status'</span><span style="color: #007700">]&nbsp;?&nbsp;</span><span style="color: #DD0000">"on"&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">"off"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Return&nbsp;success&nbsp;to&nbsp;the&nbsp;client&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">gupnp_service_action_return</span><span style="color: #007700">(</span><span style="color: #0000BB">$action</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;GetTarget&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">get_target_cb</span><span style="color: #007700">(</span><span style="color: #0000BB">$service</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$action</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arg</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">gupnp_service_action_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$action</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'RetTargetValue'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">GUPNP_TYPE_BOOLEAN</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'status'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">gupnp_service_action_return</span><span style="color: #007700">(</span><span style="color: #0000BB">$action</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;GetStatus&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">get_status_cb</span><span style="color: #007700">(</span><span style="color: #0000BB">$service</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$action</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arg</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">gupnp_service_action_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$action</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ResultStatus'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">GUPNP_TYPE_BOOLEAN</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'status'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">gupnp_service_action_return</span><span style="color: #007700">(</span><span style="color: #0000BB">$action</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;By&nbsp;default&nbsp;the&nbsp;light&nbsp;is&nbsp;off&nbsp;*/<br /></span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'status'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"The&nbsp;light&nbsp;is&nbsp;now&nbsp;%s.\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'status'</span><span style="color: #007700">]&nbsp;?&nbsp;</span><span style="color: #DD0000">"on"&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">"off"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Create&nbsp;the&nbsp;UPnP&nbsp;context&nbsp;*/<br /></span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gupnp_context_new</span><span style="color: #007700">();<br />if&nbsp;(!</span><span style="color: #0000BB">$context</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Error&nbsp;creating&nbsp;the&nbsp;GUPnP&nbsp;context\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Host&nbsp;the&nbsp;directory&nbsp;that&nbsp;contains&nbsp;device&nbsp;and&nbsp;service&nbsp;description&nbsp;files&nbsp;*/<br /></span><span style="color: #0000BB">gupnp_context_host_path</span><span style="color: #007700">(</span><span style="color: #0000BB">$context</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"./web"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Create&nbsp;root&nbsp;device&nbsp;*/<br /></span><span style="color: #0000BB">$location&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"/BinaryLight.xml"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dev&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gupnp_root_device_new</span><span style="color: #007700">(</span><span style="color: #0000BB">$context</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$location</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">gupnp_root_device_set_available</span><span style="color: #007700">(</span><span style="color: #0000BB">$dev</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Get&nbsp;the&nbsp;switch&nbsp;service&nbsp;from&nbsp;the&nbsp;root&nbsp;device&nbsp;*/<br /></span><span style="color: #0000BB">$service_type&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"urn:schemas-upnp-org:service:SwitchPower:1"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$service&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gupnp_device_info_get_service</span><span style="color: #007700">(</span><span style="color: #0000BB">$dev</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$service_type</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$service</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Cannot&nbsp;get&nbsp;SwitchPower1&nbsp;service\n"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Set&nbsp;callback&nbsp;for&nbsp;action&nbsp;GetStatus&nbsp;*/<br /></span><span style="color: #0000BB">gupnp_device_action_callback_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$service</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">GUPNP_SIGNAL_ACTION_INVOKED</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"GetStatus"</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"get_status_cb"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"action&nbsp;data,&nbsp;GetStatus"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Set&nbsp;callback&nbsp;for&nbsp;action&nbsp;GetTarget&nbsp;*/<br /></span><span style="color: #0000BB">gupnp_device_action_callback_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$service</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">GUPNP_SIGNAL_ACTION_INVOKED</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"GetTarget"</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"get_target_cb"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"action&nbsp;data,&nbsp;GetTarget"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Set&nbsp;callback&nbsp;for&nbsp;action&nbsp;SetTarget&nbsp;*/<br /></span><span style="color: #0000BB">gupnp_device_action_callback_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$service</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">GUPNP_SIGNAL_ACTION_INVOKED</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SetTarget"</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"set_target_cb"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"action&nbsp;data,&nbsp;SetTarget"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Run&nbsp;the&nbsp;main&nbsp;loop&nbsp;*/<br /></span><span style="color: #0000BB">gupnp_root_device_start</span><span style="color: #007700">(</span><span style="color: #0000BB">$dev</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-4246">
    <p><strong>Example #2 Implementing light client</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">service_proxy_available_cb</span><span style="color: #007700">(</span><span style="color: #0000BB">$proxy</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arg</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$arg</span><span style="color: #007700">[</span><span style="color: #DD0000">'mode'</span><span style="color: #007700">];<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Set&nbsp;subscribed\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">gupnp_service_proxy_set_subscribed</span><span style="color: #007700">(</span><span style="color: #0000BB">$proxy</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Add&nbsp;notify&nbsp;if&nbsp;status&nbsp;will&nbsp;be&nbsp;changed&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">gupnp_service_proxy_add_notify</span><span style="color: #007700">(</span><span style="color: #0000BB">$proxy</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Status"</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">GUPNP_TYPE_BOOLEAN</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"status_changed_cb"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Failed&nbsp;to&nbsp;add&nbsp;notify\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">'TOGGLE'</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;We're&nbsp;toggling,&nbsp;so&nbsp;first&nbsp;fetch&nbsp;the&nbsp;current&nbsp;status&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$target&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gupnp_service_proxy_action_get</span><span style="color: #007700">(</span><span style="color: #0000BB">$proxy</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'GetStatus'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ResultStatus'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">GUPNP_TYPE_BOOLEAN</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;And&nbsp;then&nbsp;toggle&nbsp;it&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$target&nbsp;</span><span style="color: #007700">=&nbsp;!&nbsp;</span><span style="color: #0000BB">$target</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Mode&nbsp;is&nbsp;a&nbsp;boolean,&nbsp;so&nbsp;the&nbsp;target&nbsp;is&nbsp;the&nbsp;mode&nbsp;thanks&nbsp;to&nbsp;our&nbsp;well&nbsp;chosen<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enumeration&nbsp;values.&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$target&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">'ON'</span><span style="color: #007700">)&nbsp;?&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Set&nbsp;the&nbsp;target&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">gupnp_service_proxy_action_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$proxy</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'SetTarget'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'NewTargetValue'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$target</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">GUPNP_TYPE_BOOLEAN</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Cannot&nbsp;set&nbsp;switch\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Set&nbsp;switch&nbsp;to&nbsp;%s.\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$target&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #DD0000">"on"&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">"off"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Stop&nbsp;browsing&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">gupnp_control_point_browse_stop</span><span style="color: #007700">(</span><span style="color: #0000BB">$arg</span><span style="color: #007700">[</span><span style="color: #DD0000">'cp'</span><span style="color: #007700">]);<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">status_changed_cb</span><span style="color: #007700">(</span><span style="color: #0000BB">$variable</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arg</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Status&nbsp;has&nbsp;been&nbsp;changed\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"\tvariable&nbsp;name:&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$variable</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"\tvalue:&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;(int)</span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Check&nbsp;and&nbsp;parse&nbsp;command&nbsp;line&nbsp;arguments&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$argv</span><span style="color: #007700">)&nbsp;!=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Usage:&nbsp;light-client.php&nbsp;[on|off|toggle]\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /><br />if&nbsp;(</span><span style="color: #0000BB">$argv</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;==&nbsp;</span><span style="color: #DD0000">"on"</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'ON'</span><span style="color: #007700">;<br />}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$argv</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;==&nbsp;</span><span style="color: #DD0000">"off"</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'OFF'</span><span style="color: #007700">;<br />}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$argv</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;==&nbsp;</span><span style="color: #DD0000">"toggle"</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'TOGGLE'</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">usage&nbsp;</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Create&nbsp;the&nbsp;UPnP&nbsp;context&nbsp;*/<br /></span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gupnp_context_new</span><span style="color: #007700">();<br />if&nbsp;(!</span><span style="color: #0000BB">$context</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Error&nbsp;creating&nbsp;the&nbsp;GUPnP&nbsp;context\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Create&nbsp;the&nbsp;control&nbsp;point,&nbsp;searching&nbsp;for&nbsp;SwitchPower&nbsp;services&nbsp;*/<br /></span><span style="color: #0000BB">$cp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gupnp_control_point_new&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$context</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"urn:schemas-upnp-org:service:SwitchPower:1"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Connect&nbsp;to&nbsp;the&nbsp;service-found&nbsp;callback&nbsp;*/<br /></span><span style="color: #0000BB">$cb&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"service_proxy_available_cb"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$arg&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'mode'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$mode</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'cp'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$cp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">gupnp_control_point_callback_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$cp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">GUPNP_SIGNAL_SERVICE_PROXY_AVAILABLE</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$cb</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arg</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Start&nbsp;for&nbsp;browsing&nbsp;*/<br /></span><span style="color: #0000BB">gupnp_control_point_browse_start</span><span style="color: #007700">(</span><span style="color: #0000BB">$cp</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="gupnp.browsing.html">Browsing devices and services</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.gupnp.html">Gupnp 函数</a></div>
 <div class="up"><a href="gupnp.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
